package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.ResultObj;
import com.luo.model.User;
import com.luo.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能：管理员修改密码
 * 作者：Luo。
 * 日期：2024/1/27 12:00
 */
@WebServlet("/adminChangePassword")
public class AdminPasswordChangeController extends HttpServlet {

    UserService userService = new UserService();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ResultObj resultObj = new ResultObj();
        String id = req.getParameter("id");
        String newPwd = req.getParameter("newPwd");
        User user = new User();
        user.setId(Integer.valueOf(id));
        user.setPassword(newPwd);
        int i = userService.adminChangePassword(user);
        resultObj.setCode(i + "");
        if (i > 0) {
            resultObj.setMsg("修改成功");
        } else {
            resultObj.setMsg("修改失败");
        }
        String jsonString = JSON.toJSONString(resultObj);
        resp.getWriter().write(jsonString);
        resp.getWriter().close();
    }
}
